package com.bst.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import com.bst.common.utils.security.CipherUtils;
import javax.crypto.SecretKey;
import javax.sql.DataSource;

@Configuration
public class DataSourceForNoteConfig {
    @Value("${note.datasource.driver-class-name}")
    String driverClass;
    @Value("${note.datasource.url}")
    String url;
    @Value("${note.datasource.username}")
    String userName;
    @Value("${note.datasource.password}")
    String passWord;

    @Bean(name = "noteDataSource")
    public DataSource noteDataSource() throws Exception {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        SecretKey secretKey = CipherUtils.strKey2SecretKey("90jpwyeTWACLU4XxT7zhkA==");
        byte[] decryResult = CipherUtils.decryptAES(CipherUtils.hexStringToBinary(passWord), secretKey);
        passWord = new String(decryResult);
        dataSource.setPassword(passWord);
        return dataSource;
    }
}
